export default class Login {
    userObj;
    form;
    ids;
    ajax;
    ipts;
    judgeInput;
    m;
    constructor() {
        this.form=document.querySelector("form");
        this.ipts=document.querySelectorAll("input")
        this.logbtn = document.getElementById("loginbtn");
        this.user=document.getElementById("userID");
        this.password=document.getElementById("password");    
        console.log(this.user.value,this.password);  
        this.m=new Map();
        // this.ajax=new AJAX
        this.bindEvent();
    }


    bindEvent(){
        this.form.addEventListener("input", e => this.inputHandler(e));
        this.logbtn.addEventListener("click", e => this.clickHandler(e))
    }
   
    async inputHandler(e){
        if(this.ids) return;
        var value = await this.clearTime(e.target);
        if(this.judgeIpt(e.target.name,value)){
            this.m.set(e.target.name,value)
            console.log( e.target.parentElement);
            e.target.parentElement.className="box green";
        }else{
            this.m.delete(e.target.name)
            console.log( e.target.parentElement);

            e.target.parentElement.className="box red";
        }
    }
    clearTime(input){
        return new Promise((resolve,reject)=>{
          this.ids=setTimeout(()=>{
              clearTimeout(this.ids);
              this.ids=undefined;
              resolve(input.value);
            },500);
        })
    }
    judgeIpt(name,value){
        switch(name){
            case 'tel':
                // return /^1\d{10}$/.test(value);
            case "password":
                // return /^(?=\D+\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9~!@#$%^&*-_+=.,";'\\\|\/]{6,16}$/.test(value);
        }
        return true;
        console.log(m);
    }
    async  clickHandler(e){
        e.preventDefault();
       
        if(this.m.size<2)return;
        var data=Object.fromEntries(this.m);
         console.log(data);
         var res=await fetch("http://localhost:4022/signIn",{method:"post",body:JSON.stringify(data)}).then(res => res.json());
         console.log(res);
        if(res){
            console.log("登录成功");
        console.log(this.m);  

        setTimeout(() => {
                location.href = "./index.html";
                sessionStorage.setItem("usermsg", JSON.stringify({
                      username: data.user,
                      password: data.password,
                }));
                console.log(sessionStorage);
          }, 2000)
           
        }else{
            console.log("登录失败");
        }
    }
   








}
new Login();



      